/**
 * 
 */
package com.huarui.desktop.service.impl;

import org.apache.commons.lang.StringUtils;

import com.huarui.desktop.model.ModuleScript;
import com.huarui.desktop.model.ModuleScriptDesc;
import com.huarui.desktop.model.ModuleScriptFunct;
import com.huarui.desktop.model.TableColumnInfo;
import com.huarui.desktop.model.TableMetaData;
import com.huarui.desktop.service.ModuleScriptBuilder;
import com.huarui.desktop.service.TableColumnResolver;

/**
 * @author sunpeng
 * 
 */
public class DefaultModuleScriptBuilder implements ModuleScriptBuilder {

	private ModuleScript moduleScript;
	private TableColumnResolver tableColumnResolver;
	private ModuleScriptBuilder subModuleScriptBuilder;

	public DefaultModuleScriptBuilder() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.huarui.desktop.service.ModuleScriptBuilder#buildCreateWindow()
	 */
	@Override
	public void buildFunctCreate(TableMetaData meta) {
		ModuleScriptDesc desc = this.moduleScript.getDesc();
		String winId = desc.getWinId();
		boolean needWinId = StringUtils.isNotBlank(winId);
		ModuleScriptFunct funtion = new ModuleScriptFunct("createWindow");
		funtion.addContentLine("var win=null;");
		funtion.addContentLine("var desktop = this.app.getDesktop();");
		if (needWinId) {
			funtion.addContentLine("win = desktop.getWindow('" + winId + "');");
		}
		funtion.addContentLine("if (!win) {");
		funtion.addContentLine("  win = desktop.createWindow({");
		if (needWinId) {
			funtion.addContentLine("	id : '" + winId + "',");
		}
		funtion.addContentLine("	title : '" + desc.getWinTitle() + "',");
		funtion.addContentLine("	iconCls : '" + desc.getWinIconCls() + "',");
		funtion.addContentLine("	taskbuttonTooltip: '" + desc.getWinTitle() + "',");
		funtion.addContentLine("	width : '100%',");
		funtion.addContentLine("	shim : false,");
		funtion.addContentLine("	animCollapse : false,");
		funtion.addContentLine("	constrainHeader : true,");
		funtion.addContentLine("	maximizable : true,");
		funtion.addContentLine("	maximized : true,");
		funtion.addContentLine("	items : this.createElements()");
		funtion.addContentLine("  });");
		funtion.addContentLine("}");
		funtion.addContentLine("win.show();");

		this.moduleScript.setFunctCreate(funtion);

		this.buildCreateElements(desc, meta);
	}

	protected void buildCreateElements(ModuleScriptDesc desc, TableMetaData meta) {
		TableColumnInfo[] columns = meta.getColumns();
		ModuleScriptFunct function = new ModuleScriptFunct("createElements");

		function.addContentLine("var gridStore= new Ext.data.Store({");
		function.addContentLine("	proxy : new Ext.data.HttpProxy({url : Uec3.App.Constants.homeUrl }),");
		function.addContentLine("	baseParams : {start : 0,limit : 20},");
		function.addContentLine("	remoteSort : true,");
		function.addContentLine("	reader : new Ext.data.JsonReader({totalProperty : 'total',root : 'items'},");
		function.addContentLine("		" + this.buildGridColumnReader(columns));
		function.addContentLine("	)");
		function.addContentLine("});");

		function.addContentLine("var gridColumnModel= new Ext.grid.ColumnModel(");
		function.addContentLine("	" + this.buildGridColumnModel(columns));
		function.addContentLine(");");

		function.addContentLine("var gridPanel=new Ext.grid.GridPanel({");
		function.addContentLine("	enableColumnMove : false,");
		function.addContentLine("	enableColumnMoveResize : false,");
		function.addContentLine("	stripeRows : true,");
		function.addContentLine("	loadMask : {msg : '...加载数据中'},");
		function.addContentLine("	viewConfig : {forceFit : true,columnsText : '显示的列',scrollOffset : 20,sortAscText : '升序',sortDescText : '降序'},");
		function.addContentLine("	tbar : new Ext.Toolbar(['-', {text : '删除数据',handler : function() {}}]),");
		function.addContentLine("	bbar : new Ext.PagingToolbar({pageSize : 20,store : gridStore,displayInfo : true,displayMsg : '第 {0} - {1} 条  共 {2} 条',emptyMsg : '没有记录'}),");
		function.addContentLine("	iconCls : '" + desc.getWinIconCls() + "',");
		function.addContentLine("	title : '" + desc.getWinTitle() + "',");
		function.addContentLine("	cm:gridColumnModel,");
		function.addContentLine("	store:gridStore");
		function.addContentLine("});");

		function.addContentLine("gridStore.load();");
		function.addContentLine("return gridPanel;");

		this.moduleScript.addFunctOthers(function);
	}

	protected String buildGridColumnModel(TableColumnInfo[] columns) {

		String[] result = new String[columns.length];
		for (int i = 0; i < columns.length; i++) {
			result[i] = this.tableColumnResolver.getRecordMode(columns[i]);
		}
		return "[" + StringUtils.join(result, ",") + "]";
	}

	protected String buildGridColumnReader(TableColumnInfo[] columns) {

		String[] result = new String[columns.length];
		for (int i = 0; i < columns.length; i++) {

			result[i] = this.tableColumnResolver.getRecordType(columns[i]);
		}

		return "[" + StringUtils.join(result, ",") + "]";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.huarui.desktop.service.ModuleScriptBuilder#buildInit()
	 */
	@Override
	public void buildFunctInit(TableMetaData meta) {
		ModuleScriptFunct funtion = new ModuleScriptFunct("init");
		funtion.setParams(new String[] { "initParams" });
		this.moduleScript.setFunctInit(funtion);
	}

	@Override
	public void buildFunctOthers(TableMetaData meta) {
		// TODO Auto-generated method stub

	}

	@Override
	public void buildSubclass(ModuleScriptDesc desc, TableMetaData meta) {

		if (this.subModuleScriptBuilder != null) {
			this.subModuleScriptBuilder.initScript(desc);
			this.subModuleScriptBuilder.buildFunctInit(meta);
			this.subModuleScriptBuilder.buildFunctCreate(meta);
			this.subModuleScriptBuilder.buildFunctOthers(meta);
			ModuleScript subModuleScript = this.subModuleScriptBuilder.getModuleScript();
			this.moduleScript.setSubModuleScript(subModuleScript);
		}
	}

	@Override
	public void initScript(ModuleScriptDesc desc) {
		this.moduleScript = new ModuleScript();
		this.moduleScript.setDesc(desc);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.huarui.desktop.service.ModuleScriptBuilder#getModuleScript()
	 */
	@Override
	public ModuleScript getModuleScript() {
		return this.moduleScript;
	}

	public void setTableColumnResolver(TableColumnResolver tableColumnResolver) {
		this.tableColumnResolver = tableColumnResolver;
	}

	public void setSubModuleScriptBuilder(ModuleScriptBuilder subModuleScriptBuilder) {
		this.subModuleScriptBuilder = subModuleScriptBuilder;
	}

}
